package action;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import entitybean.l_sequence.EntityBeanL_Sequence;
import entitybean.oc_user_name.EntityBeanOc_User_Name;
import entitybean.systemtree.EntityBeanSystemtree;
import form.UserInfoForm;
import service.service_admin.Service_Admin;
import service.service_public.Service_Public;
import tools.InterfaceForResultSetToList;
import tools.PageBeanPlus;
import view.ViewOc_User_Info;


public class Action_Oc_Admin_Info extends DispatchAction {
	private Service_Admin service_admin;
	private Service_Public service_public;
	public Service_Admin getService_admin() {
		return service_admin;
	}
	public void setService_admin(Service_Admin serviceAdmin) {
		service_admin = serviceAdmin;
	}
	public Service_Public getService_public() {
		return service_public;
	}
	public void setService_public(Service_Public servicePublic) {
		service_public = servicePublic;
	}
	public ActionForward admin_init_admin_info(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		System.out.println("初始化海洋生物的航次");
		String sql="SELECT ID_CARD,ID,USERNAME,LOGIN_TIME,LAST_ACTION,EMAIL,PASSWORD,REALNAME,ISACTIVE,BACKUP FROM OC_USER_NAME WHERE DEL='OFF' AND POWER<>'OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(userInfoform.getRowcountperpage()), Integer
						.valueOf(userInfoform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_User_Info> list = interfaceforresultsettolist
				.ViewOc_User_Info(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("userInfoform", userInfoform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}
	public ActionForward admin_search_admin_info(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		System.out.println("初始化海洋生物的航次");
		String sql="SELECT ID_CARD,ID,USERNAME,LOGIN_TIME,LAST_ACTION,EMAIL,PASSWORD,REALNAME,ISACTIVE,BACKUP FROM OC_USER_NAME WHERE DEL='OFF' AND POWER<>'OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(userInfoform.getRowcountperpage()), Integer
						.valueOf(userInfoform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_User_Info> list = interfaceforresultsettolist
				.ViewOc_User_Info(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("userInfoform", userInfoform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}
	
	public ActionForward admin_load_admin_info(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		String id=userInfoform.getId();
		System.out.println("ID="+id);		
		EntityBeanOc_User_Name e=new EntityBeanOc_User_Name();
		e=this.service_admin.Find_EntityBeanOc_User_Name_By_ID(Integer.valueOf(id));
		request.setAttribute("userInfoform", userInfoform);
		request.setAttribute("e", e);
		return mapping.findForward("success");
	}
	public ActionForward admin_update_admin_info(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		String id=userInfoform.getId();
		EntityBeanOc_User_Name e=new EntityBeanOc_User_Name();
		e=this.service_admin.Find_EntityBeanOc_User_Name_By_ID(Integer.valueOf(id));
		
		
		
		String backup=userInfoform.getBackup();
		String email= userInfoform.getEmail();
		String idCard=userInfoform.getId_card();
		String	isactive =userInfoform.getIsactive();
		String	last_action =userInfoform.getLast_action();
		String password =userInfoform.getPassword();
		String	realname =userInfoform.getRealname();
		String	username=userInfoform.getUsername();
		System.out.println("last_action="+last_action);
		last_action=last_action.replace("年", "-");
		last_action=last_action.replace("月", "-");
		last_action=last_action.replace("日", "");
		last_action=last_action+" 23:59:59";
		System.out.println("last_action="+last_action);
		Date D=new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
		"yyyy-MM-dd HH:mm:ss");
		D= simpleDateFormat.parse(last_action);
		e.setBackup(backup);
		e.setDel("OFF");
		e.setEmail(email);
		e.setId_card(idCard);
		e.setIsactive(isactive);
		e.setLast_action(D);
		e.setPassword(password);
		e.setLogin_time(new Date());
		e.setRealname(realname);
		e.setUsername(username);
		this.service_admin.Update_Oc_User_Name(e);
		
		
		System.out.println("初始化海洋生物的航次");
		String sql="SELECT ID_CARD,ID,USERNAME,LOGIN_TIME,LAST_ACTION,EMAIL,PASSWORD,REALNAME,ISACTIVE,BACKUP FROM OC_USER_NAME WHERE DEL='OFF' AND POWER<>'OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(userInfoform.getRowcountperpage()), Integer
						.valueOf(userInfoform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_User_Info> list = interfaceforresultsettolist
				.ViewOc_User_Info(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("userInfoform", userInfoform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}
	private String cut_tail(String str) {
		String cut = str;
		if (str.endsWith(",")) {
			cut = cut.substring(0, cut.length() - 1);
		}
		return cut;
	}
	public ActionForward admin_delete_admin_info(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		String id=userInfoform.getId();
		System.out.println("ID="+id);
		id=this.cut_tail(id);
		this.service_admin.Delete_Oc_User_Name(id);
		System.out.println("初始化海洋生物的航次");
		String sql="SELECT ID_CARD,ID,USERNAME,LOGIN_TIME,LAST_ACTION,EMAIL,PASSWORD,REALNAME,ISACTIVE,BACKUP FROM OC_USER_NAME WHERE DEL='OFF' AND POWER<>'OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(userInfoform.getRowcountperpage()), Integer
						.valueOf(userInfoform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_User_Info> list = interfaceforresultsettolist
				.ViewOc_User_Info(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("userInfoform", userInfoform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}
	
	public ActionForward admin_forbid_admin_info(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		String id=userInfoform.getId();
		System.out.println("ID="+id);
		id=this.cut_tail(id);
		this.service_admin.Forbid_Oc_User_Name(id);
		System.out.println("初始化海洋生物的航次");
		String sql="SELECT ID_CARD,ID,USERNAME,LOGIN_TIME,LAST_ACTION,EMAIL,PASSWORD,REALNAME,ISACTIVE,BACKUP FROM OC_USER_NAME WHERE DEL='OFF' AND POWER<>'OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(userInfoform.getRowcountperpage()), Integer
						.valueOf(userInfoform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_User_Info> list = interfaceforresultsettolist
				.ViewOc_User_Info(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("userInfoform", userInfoform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}
	public ActionForward admin_active_admin_info(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		String id=userInfoform.getId();
		System.out.println("ID="+id);
		id=this.cut_tail(id);
		this.service_admin.Active_Oc_User_Name(id);
		System.out.println("初始化海洋生物的航次");
		String sql="SELECT ID_CARD,ID,USERNAME,LOGIN_TIME,LAST_ACTION,EMAIL,PASSWORD,REALNAME,ISACTIVE,BACKUP FROM OC_USER_NAME WHERE DEL='OFF' AND POWER<>'OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(userInfoform.getRowcountperpage()), Integer
						.valueOf(userInfoform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_User_Info> list = interfaceforresultsettolist
				.ViewOc_User_Info(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("userInfoform", userInfoform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}
	
	public ActionForward admin_add_admin_info(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		request.setAttribute("userInfoform", userInfoform);
		return mapping.findForward("success");
	}
	public ActionForward admin_insert_admin_info(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		EntityBeanOc_User_Name e=new EntityBeanOc_User_Name();
		String backup=userInfoform.getBackup();
		String email= userInfoform.getEmail();
		String idCard=userInfoform.getId_card();
		String	isactive =userInfoform.getIsactive();
		String	last_action =userInfoform.getLast_action();
		String password =userInfoform.getPassword();
		String	realname =userInfoform.getRealname();
		String	username=userInfoform.getUsername();
		System.out.println("last_action="+last_action);
		last_action=last_action.replace("年", "-");
		last_action=last_action.replace("月", "-");
		last_action=last_action.replace("日", "");
		last_action=last_action+" 23:59:59";
		System.out.println("last_action="+last_action);
		Date D=new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
		"yyyy-MM-dd HH:mm:ss");
		D= simpleDateFormat.parse(last_action);
		e.setBackup(backup);
		e.setDel("OFF");
		e.setEmail(email);
		e.setId_card(idCard);
		e.setIsactive(isactive);
		e.setLast_action(D);
		e.setPassword(password);
		e.setLogin_time(new Date());
		e.setPower("000");
		e.setRealname(realname);
		e.setUsername(username);
		EntityBeanL_Sequence l = this.service_public
		.Find_L_Sequence_By_seq_Name("SEQ_OC_USERNAME");
		Integer id = l.getSeq_Value() + 1;
		l.setSeq_Value(id);
		e.setId(id);
		this.service_admin.Insert_Oc_User_Name(e);
		this.service_public.Update_L_Sequence(l);
		
		System.out.println("初始化海洋生物的航次");
		String sql="SELECT ID_CARD,ID,USERNAME,LOGIN_TIME,LAST_ACTION,EMAIL,PASSWORD,REALNAME,ISACTIVE,BACKUP FROM OC_USER_NAME WHERE DEL='OFF' AND POWER<>'OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(userInfoform.getRowcountperpage()), Integer
						.valueOf(userInfoform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_User_Info> list = interfaceforresultsettolist
				.ViewOc_User_Info(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("userInfoform", userInfoform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}
	public ActionForward admin_load_power_admin_info(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		
		List<EntityBeanSystemtree>list=this.service_admin.Find_SystemTree_By_c_jd(0);
		String id=userInfoform.getId();
		System.out.println("ID="+id);		
		EntityBeanOc_User_Name e=new EntityBeanOc_User_Name();
		e=this.service_admin.Find_EntityBeanOc_User_Name_By_ID(Integer.valueOf(id));
		String a[]=e.getPower().split(",");
		System.out.println(a.length);
		List<String>power_e=new ArrayList<String>();
		for(int i=0;i<a.length;i++)
		{
			power_e.add(a[i]);
			
			
		}
		request.setAttribute("e", e);
		request.setAttribute("power_e", power_e);
		request.setAttribute("list", list);
		request.setAttribute("userInfoform", userInfoform);
		return mapping.findForward("success");
	}
	public ActionForward admin_update_power_admin_info(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		String id=userInfoform.getId();
		String ss=userInfoform.getLast_action();
		System.out.println("SS="+ss);
		if (ss==null||ss.equals(""))
		{
			ss="ON";
		}
		ss=this.cut_tail(ss);
		EntityBeanOc_User_Name e=new EntityBeanOc_User_Name();
		e=this.service_admin.Find_EntityBeanOc_User_Name_By_ID(Integer.valueOf(id));
		e.setPower(ss);
		this.service_admin.Update_Oc_User_Name(e);
		System.out.println("初始化海洋生物的航次");
		String sql="SELECT ID_CARD,ID,USERNAME,LOGIN_TIME,LAST_ACTION,EMAIL,PASSWORD,REALNAME,ISACTIVE,BACKUP FROM OC_USER_NAME WHERE DEL='OFF' AND POWER<>'OFF'";
		PageBeanPlus pagebeanplus = new PageBeanPlus("oracle", "sql", sql,
				Integer.valueOf(userInfoform.getRowcountperpage()), Integer
						.valueOf(userInfoform.getCurrentpage()), "loadrichtext()");
		InterfaceForResultSetToList interfaceforresultsettolist = new InterfaceForResultSetToList();
		List<ViewOc_User_Info> list = interfaceforresultsettolist
				.ViewOc_User_Info(pagebeanplus.getFormatqueryresultset());

		request.setAttribute("pagebeanplus", pagebeanplus);// 设置传递到页面的数据
		request.setAttribute("list", list);
		request.setAttribute("userInfoform", userInfoform);
		System.out.println("list.size()" + list.size());
		return mapping.findForward("success");
	}
	public ActionForward admin_insert_admin_info_front(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		System.out.println("编辑会员信息");
		UserInfoForm userInfoform = (UserInfoForm) form;
		EntityBeanOc_User_Name e=new EntityBeanOc_User_Name();
		String backup=userInfoform.getBackup();
		String email= userInfoform.getEmail();
		String idCard=userInfoform.getId_card();
		String password =userInfoform.getPassword();
		String	realname =userInfoform.getRealname();
		String	username=userInfoform.getUsername();
		String verifyCode=userInfoform.getVerifyCode();
		List<EntityBeanOc_User_Name> listname=this.service_admin.Find_EntityBeanOcByName(username);
		List<EntityBeanOc_User_Name> listmail=this.service_admin.Find_EntityBeanOcByMail(email);
		if(listname.size()>0){
			request.setAttribute("msg", "1");
			return mapping.findForward("auto");
		}
		if(listmail.size()>0){
			request.setAttribute("msg", "2");
			return mapping.findForward("auto");
		}
		String picdode=(String)request.getSession().getAttribute("piccode");
		System.out.print(picdode.toLowerCase());
		if(!picdode.equals(verifyCode.toUpperCase())&&picdode!=verifyCode.toUpperCase()){
			request.setAttribute("msg", "4");
			return mapping.findForward("auto");
		}
		e.setBackup(backup);
		e.setDel("OFF");
		e.setEmail(email);
		e.setId_card(idCard);
		e.setIsactive("OFF");
		e.setLast_action(new Date());
		e.setPassword(password);
		e.setLogin_time(new Date());
		e.setPower("OFF");
		e.setRealname(realname);
		e.setUsername(username);
		EntityBeanL_Sequence l = this.service_public.Find_L_Sequence_By_seq_Name("SEQ_OC_USERNAME");
		Integer id = l.getSeq_Value() + 1;
		l.setSeq_Value(id);
		e.setId(id);
		this.service_admin.Insert_Oc_User_Name(e);
		this.service_public.Update_L_Sequence(l);
		request.setAttribute("msg", "3");
		return mapping.findForward("success");
		
	}
}
